<?php 
if (!isset($_SESSION)) {
  session_start();
  if(!isset($_SESSION['MM_Username']))
  	header("location:../../index.php");
}
	require_once('../../Connections/microassur.php');
	require_once('../../utils/fonctions.php');
	
/********************************DEBUT FONCTIONS****************************/
function idGarantie_garantiesouscrite($database_microassur, $microassur, $Identifiant_contrat){
	$query_rs = "SELECT Identifiant_garantie FROM garantiesouscrite WHERE Identifiant_contrat = $Identifiant_contrat";
	$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
	$row_rs = mysql_fetch_assoc($rs);
	mysql_free_result($rs);
	return $row_rs['Identifiant_garantie'];
}

function idProduit_garantie($database_microassur, $microassur, $Identifiant_garantie){
	$query_rs = "SELECT Identifiant_produit FROM garantie WHERE Identifiant_garantie = $Identifiant_garantie";
	$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
	$row_rs = mysql_fetch_assoc($rs);
	mysql_free_result($rs);
	return $row_rs['Identifiant_produit'];
}

function idOffre_produit($database_microassur, $microassur, $Identifiant_produit){
	$query_rs = "SELECT Identifiant_offre FROM produit WHERE Identifiant_produit = $Identifiant_produit";
	$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
	$row_rs = mysql_fetch_assoc($rs);
	mysql_free_result($rs);
	return $row_rs['Identifiant_offre'];
}
/********************************FIN FONCTIONS****************************/

?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if(isset($_GET['modif']))
	$modif = $_GET['modif'];
else
	$modif = 0;

$colname_rs1 = "-1";
$idContrat = "-1";
if (isset($_GET['Identifiant_personne'])) {
  $colname_rs1 = $_GET['Identifiant_personne'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs1 = sprintf("SELECT * FROM personne WHERE Identifiant_personne = %s", GetSQLValueString($colname_rs1, "int"));
$rs1 = mysql_query($query_rs1, $microassur) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1);

// ************DEBUT
if(isset($_GET['Identifiant_personne']) && isset($_GET['Numero_contrat'])) {
  $idContrat = IdContrat($database_microassur, $microassur, $_GET['Numero_contrat']);
  if(isset($_POST['Date_naissance_personne']) && !empty($_POST['Date_naissance_personne']))
	$dateNaissance = dateEN2FR(remind_text('Date_naissance_personne'));
  else
	$dateNaissance = dateNaissancePersonne($database_microassur, $microassur, $_GET['Identifiant_personne']);
  // var_dump($dateNaissance);
}  

if(isset($idContrat)){
	$idGarantie = idGarantie_garantiesouscrite($database_microassur, $microassur, GetSQLValueString($idContrat, "int"));
}
if(isset($idGarantie)){
	$idProduit = idProduit_garantie($database_microassur, $microassur, intval($idGarantie));
}
if(isset($idProduit)){
	$idOffre = idOffre_produit($database_microassur, $microassur, $idProduit);
}

$age = age($dateNaissance); //var_dump($age);

if(isset($colname_rs1) && isset($idContrat)){
  $typeAssure = typeAssureRole2($database_microassur, $microassur, $colname_rs1, $idContrat);
  //$idOffre = idOffreRole2($database_microassur, $microassur, $colname_rs1, $idContrat);
  $etatRole = etatRole($database_microassur, $microassur, $colname_rs1, $idContrat);
}

mysql_select_db($database_microassur, $microassur);
$query_rs2 = "SELECT * FROM conditionvente WHERE Type_condition_vente = 'L' AND Identifiant_offre = '". $idOffre ."' AND Identifiant_offre IS NOT NULL";
$rs21 = mysql_query($query_rs2, $microassur) or die(mysql_error());
$rs2 = mysql_query($query_rs2, $microassur) or die(mysql_error());
//$row_rs2 = mysql_fetch_assoc($rs2);
$totalRows_rs2 = mysql_num_rows($rs2);
//***********FIN

$probleme_age = false;
while($row_rs2 = mysql_fetch_assoc($rs2)){ 

	if(strcmp($row_rs2['Code_condition_vente'], 'AGMIA' )==0 || strcmp($row_rs2['Code_condition_vente'], 'AGMIE' )==0 || strcmp($row_rs2['Code_condition_vente'], 'AGMAA' )==0 || strcmp($row_rs2['Code_condition_vente'], 'AGMAE' )==0){
		if(($etatRole!='S') && $typeAssure!='E' && strcmp($row_rs2['Code_condition_vente'], 'AGMIA' )==0 && ($age < intval($row_rs2['Valeur_condition_vente']))){
			$message = colorer("La personne est plus jeune que l’âge requis à l’adhésion.", "false");
			$probleme_age = true;
		}
		if(($etatRole!='S') && $typeAssure!='E' && strcmp($row_rs2['Code_condition_vente'], 'AGMAA' )==0 && ($age > intval($row_rs2['Valeur_condition_vente']))){
			$message = colorer("La personne a dépassé l’âge limite à l’adhésion.", "false");
			$probleme_age = true;
			 //var_dump($row_rs2['Code_condition_vente']);
			 //var_dump($row_rs2['Valeur_condition_vente']);
		}
		if(($etatRole!='S') && $typeAssure=='E' && strcmp($row_rs2['Code_condition_vente'], 'AGMIE' )==0 && ($age < intval($row_rs2['Valeur_condition_vente']))){
			$message = colorer("La personne est plus jeune que l’âge requis à l’adhésion.", "false");
			$probleme_age = true;
			 //var_dump($row_rs2['Code_condition_vente']);
			 //var_dump($row_rs2['Valeur_condition_vente']);
		}
		if(($etatRole!='S') && $typeAssure=='E' && strcmp($row_rs2['Code_condition_vente'], 'AGMAE' )==0 && ($age > intval($row_rs2['Valeur_condition_vente']))){
			$message = colorer("La personne a dépassé l’âge limite à l’adhésion.", "false");
			$probleme_age = true;
			 //var_dump($row_rs2['Code_condition_vente']);
			 //var_dump($row_rs2['Valeur_condition_vente']);
		}
		if($etatRole=='S'){
			$message = colorer("Vous ne pouvez modifier un intervenant sinistré.", "false");
			$probleme_age = true;
			 //var_dump($row_rs2['Code_condition_vente']);
			 //var_dump($row_rs2['Valeur_condition_vente']);
		}
	}
}

//var_dump(datevalider($_POST['Date_naissance_personne']));	
if(isset($_POST['Date_naissance_personne']) && empty($_POST['Date_naissance_personne'])){
	$message = colorer("Vous devez saisir la date de naissance.", "false");
}elseif(!empty($_POST['Date_naissance_personne']) && !datevalider($_POST['Date_naissance_personne'])){
	$message = colorer("Le format de la date de naissance est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année.", "false");
}elseif(isset($_POST['Date_naissance_personne']) && !datevalide($_POST['Date_naissance_personne'])){
	$message = colorer("La date de naissance est incohérente.", "false");
}else{ 
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1") && ($probleme_age == false)) {
	  $updateSQL = sprintf("UPDATE personne SET Date_naissance_personne=%s WHERE Identifiant_personne=%s",
						   GetSQLValueString(dateEN2FR($_POST['Date_naissance_personne']), "date"),
						   GetSQLValueString($_POST['Identifiant_personne'], "int"));

	  mysql_select_db($database_microassur, $microassur);
	  mysql_query("START TRANSACTION");
	  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
	  if($Result1){
		$updateSQL2 = sprintf("UPDATE role SET Etat_role='A' WHERE Identifiant_contrat=%s AND Identifiant_personne=%s",
						GetSQLValueString($idContrat, "int"),
						GetSQLValueString($_GET['Identifiant_personne'], "int"));
		$Result2 = mysql_query($updateSQL2, $microassur) or die(mysql_error());
		if($Result2){
			$message = colorer("La modification de la date de naissance est prise en compte.", "false");
		}else{
			$message = colorer("Problème modification date de naissance, réactivation contrat, contactez la DSI.", "false");
		}
		$modif=1;
		mysql_query("commit");
	  }else{
		mysql_query("rollback");
		$message = colorer("Problème modification personne, contacter la DSI.", "false");
	  }
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title>Détail intervenant</title>
			<link rel="stylesheet" type="text/css" href="../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
            <link type="text/css" href="../../css/custom-theme/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	
            <script type="text/javascript" src="../../js/jquery-1.5.1.min.js"></script>
            <script type="text/javascript" src="../../js/jquery-ui-1.8.11.custom.min.js"></script>
            <script type="text/javascript" src="../../js/jquery.ui.datepicker-fr.js"></script>
            <script type="text/javascript" src="../../js/scripts.js"></script>
	</head>

<body>
	<div id="sf_menu1">
		<h3>Production</h3>
		<?php 
			if( isset($message) && !empty($message) ){
				echo $message;
				unset($age);
			} 
			$nommodule="Réactivation contrat";
			$nommodule2="Détail intervenant";
			//$nom_module_saisie="Réactivation contrat";
		?>
	</div>
	<div class="cleared"></div>
	<div class="corps1">
			<!--/***************************************/-->
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
	<div class="imgleft"></div>
	<div class="imgmiddle">
		<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
		<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
		<div class="cleared"></div>
		<div class="formulaire">
		  <table class="tabform">
			<tr>
			  <td>NOM</td>
			  <td><input type="text" name="Nom_personne" value="<?php if(isset($_POST['Nom_personne'])){ echo remind_text('Nom_personne'); }else{ echo htmlentities($row_rs1['Nom_personne'], ENT_COMPAT, 'utf-8');} ?>" size="32" disabled="disabled" /></td>
			</tr>
			<tr>
			  <td>PRENOM</td>
			  <td><input type="text" name="Prenom_personne" value="<?php if(isset($_POST['Prenom_personne'])){ echo remind_text('Prenom_personne'); }else{ echo htmlentities($row_rs1['Prenom_personne'], ENT_COMPAT, 'utf-8');} ?>" size="32" disabled="disabled" /></td>
			</tr>
			<tr>
			  <td>TYPE ASSURE</td>
			  <?php $tab_typeAssure = array('AS'=>'Ascendant', 'AU'=>'Autre', 'C'=>'Conjoint', 'E'=>'Enfant', 'S'=>'Souscripteur'); ?>
			  <td><input type="text" name="Type_assure" value="<?php if(isset($typeAssure)) echo $tab_typeAssure[$typeAssure]; ?>" size="32" disabled="disabled" /></td>
			</tr>
			<tr>
			  <td>DATE DE NAISSANCE</td>
			  <td><input type="text" name="Date_naissance_personne" class="dateFo" value="<?php if(isset($_POST['Date_naissance_personne'])){ echo remind_text('Date_naissance_personne'); }else{ echo htmlentities(dateEN2FR($row_rs1['Date_naissance_personne']), ENT_COMPAT, 'utf-8');} ?>" size="32" maxlength="10" /></td>
			</tr>
			<tr>
			  <td>LIEU DE NAISSANCE</td>
			  <td><input type="text" name="Lieu_naissance_personne" value="<?php if(isset($_POST['Lieu_naissance_personne'])){ echo remind_text('Lieu_naissance_personne'); }else{ echo htmlentities($row_rs1['Lieu_naissance_personne'], ENT_COMPAT, 'utf-8');} ?>" size="32" disabled="disabled" /></td>
			</tr>
			<!--tr>
			  <td>&nbsp;</td>
			  <td>
				<input type="submit" value="Valider" />&nbsp;
				<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $_GET['Numero_contrat']; ?>&modif=<?php echo $modif; ?>">Annuler</a>&nbsp;
				<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $_GET['Numero_contrat']; ?>&modif=<?php echo $modif; ?>">Retour</a>
			  </td>
			</tr-->
		  </table>
	  </div>
	  <input type="hidden" name="Identifiant_personne" value="<?php if(isset($_POST['Identifiant_personne'])){ echo $_POST['Identifiant_personne']; }else{ echo $row_rs1['Identifiant_personne'];} ?>" />
	  <input type="hidden" name="Type_personne" value="<?php echo htmlentities($row_rs1['Type_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Etat_personne" value="<?php echo htmlentities($row_rs1['Etat_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Civilite_personne" value="<?php echo htmlentities($row_rs1['Civilite_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Nom_jeune_fille_personne" value="<?php echo htmlentities($row_rs1['Nom_jeune_fille_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Raison_sociale" value="<?php echo htmlentities($row_rs1['Raison_sociale'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Sexe_personne" value="<?php echo htmlentities($row_rs1['Sexe_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Profession_personne" value="<?php echo htmlentities($row_rs1['Profession_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Employeur_personne" value="<?php echo htmlentities($row_rs1['Employeur_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="MM_update" value="form1" />
	  <input type="hidden" name="Identifiant_personne" value="<?php echo $row_rs1['Identifiant_personne']; ?>" />

	</div>
	<div class="imgright"></div>

	<div class="cleared"></div>
	<!--/***************************************/-->
	<div id="br"></div>

  <div class="bt1">
	<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $_GET['Numero_contrat']; ?>&modif=<?php echo $modif; ?>">
    	<span class="btleft"></span>
        <span class="btmiddle">Retour</span>
        <span class="btright"></span>
    </a>
  </div>
  <div class="bt">
	<?php if(isset($_GET['modif']) && $_GET['modif']==1){ ?>
	<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $_GET['Numero_contrat']; ?>&modif=1">
	<?php }else{ ?>
	<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $_GET['Numero_contrat']; ?>&modif=<?php echo $modif; ?>">
	<?php } ?>
    	<span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a>
  </div>
  <div class="bt">
	<span class="btleft"></span>
	<?php if(isset($_GET['visu']) && $_GET['visu']==0){ ?>
	<input type="submit" name="Valider" value="Valider" class="resizeall btmiddle"/>
	<?php }else{ ?>
	<span class="btmiddle"><a href="#">Valider</a></span>
	<?php } ?>
	<span class="btright"></span>
  </div>
</form>

 </div>
</body>
</html>
<?php
mysql_free_result($rs1);
?>
